/*
   Copyright 2011-2012 gtalent2@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package world

import (
	"../models"
	graphics "github.com/gtalent/starfish/gfx"
	"github.com/gtalent/starfish/util"
	"sync"
)

type GenericSprite struct {
	lockt        sync.Mutex
	animLayerSet int
	animLayers   [][]AnimLayer
	location     util.Point
	layer        int
	name         string
}

func loadGenericSprite(ls *models.Sprite) *GenericSprite {
	gs := new(GenericSprite)
	gs.animLayers = make([][]AnimLayer, len(ls.AnimLayers))
	for i, v := range ls.AnimLayers {
		for _, v2 := range v {
			gs.animLayers[i] = append(gs.animLayers[i], loadAnimLayer(v2))
		}
	}
	gs.name = ls.Name
	return gs
}

func (me *GenericSprite) lock() {
	me.lockt.Lock()
}

func (me *GenericSprite) unlock() {
	me.lockt.Unlock()
}

func (me *GenericSprite) isMoving() bool {
	return false
}

func (me *GenericSprite) Location() util.Point {
	return me.location
}

func (me *GenericSprite) SetLocation(x, y int) {
	me.location.X = x
	me.location.Y = y
}

func (me *GenericSprite) Layer() int {
	return me.layer
}

func (me *GenericSprite) SetLayer(l int) {
	me.layer = l
}

func (me *GenericSprite) Name() string {
	return me.name
}

func (me *GenericSprite) Draw(canvas *graphics.Canvas, loc util.Point) {
	animLayerSet := me.animLayerSet
	for _, v := range me.animLayers[animLayerSet] {
		canvas.DrawAnimation(v.Animation.Animation, v.X+loc.X, v.Y+loc.Y)
	}
}

func (me *GenericSprite) Free() {
	for _, v := range me.animLayers {
		for _, v2 := range v {
			v2.Free()
		}
	}
}
